package com.haixinga.commbiz.commons.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.haixinga.commbiz.commons.entity.SysUser;
import com.haixinga.commbiz.commons.entity.SysUserAuth;
import com.haixinga.commbiz.commons.exception.BizException;
import com.haixinga.commbiz.commons.mapper.SysUserMapper;
import com.haixinga.commbiz.commons.utils.MD5Kit;

/**
 * <p>
 * 系统操作员表 服务实现类
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2020-06-13
 */
@Service
public class SysUserService extends ServiceImpl<SysUserMapper, SysUser> {

    @Autowired private SysUserAuthService sysUserAuthService;


    //账号密码验证
    public SysUser accountValidate(String account, String pwdRaw){

        //TODO 1 需要自动判断登录账号类型
        Integer identityType = 1;
        if(true){
            //... TODO 1
        }


        //首先根据登录类型 + 用户名得到 信息
        SysUserAuth auth = sysUserAuthService.selectByLogin(account, identityType);

        if(auth == null){ //没有该用户信息
            throw new BizException("用户名/密码错误！");
        }

        // 验证密码是否正确
        if(!auth.getCredential().equals(MD5Kit.processPwd(pwdRaw, auth.getSalt()))){  //密码错误！
            throw new BizException("用户名/密码错误！");
        }

        return getById(auth.getUserId());
    }



}
